package com.dattplace.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dattplace.dao.IUserDao;
import com.dattplace.model.ResultCode;
import com.dattplace.model.ResultData;
import com.dattplace.model.User;
import com.dattplace.service.IUserService;
import com.dattplace.vo.MenuVo;
import com.dattplace.vo.UserVo;


@Service("userService")
public class UserService implements IUserService {

	@Autowired
	private IUserDao userDao;

	@Autowired
	private Mapper mapper;

	@Override
	public UserVo login() {

		User user = userDao.querUser();

		if (user == null) {

			return null;
		}
		return mapper.map(user, UserVo.class);
	}

	@Override
	public User queryUserByName(String userName) {

		return userDao.queryUserByName(userName);
	}

	@Override
	public List<MenuVo> getUserMeuns(User user) {

		List<MenuVo> menus = new ArrayList<MenuVo>();
		
		if (user.getUserType() == 1 || user.getUserType()==2) {
			
			MenuVo menu1 = new MenuVo();
			menu1.setMarkName("userManagement");
			menus.add(menu1);
			
			MenuVo menu2 = new MenuVo();
			menu2.setMarkName("labelManagement");
			menus.add(menu2);
			
			MenuVo menu3 = new MenuVo();
			menu3.setMarkName("shareManagement");
			menus.add(menu3);
			
			MenuVo menu4 = new MenuVo();
			menu4.setMarkName("autoUpdateManagement");
			menus.add(menu4);
			
			/*MenuVo menu5 = new MenuVo();
			menu5.setMarkName("merchantManagement");// 用商户管�?
			menus.add(menu5);

			MenuVo menu6 = new MenuVo();
			menu6.setMarkName("smallNoManagement");// 小号
			menus.add(menu6);*/
		}
		return menus;
	}

	@Override
	public void saveUsers(List<User> list) {

		if (CollectionUtils.isNotEmpty(list)) {

			for (User user : list) {
				userDao.saveUsers(user);
			}
		}
	}

	@Override
	public void deleteUserById(String userId) {
		userDao.deleteUserById(userId);
	}

	@Override
	public void cleanUserLoginInfo(String userId) {
		
		userDao.cleanUserLoginInfo(userId);
	}

	@Override
	public void updateUserLoginInfo(String accessToken, Date lastLoginTime,long expireTime,String userId) {
		userDao.updateUserLoginInfo(accessToken,lastLoginTime,expireTime,userId);
	}

	@Override
	public UserVo queryUserById(String userId) {
			
		User user = userDao.queryUserById(userId);
		if (user == null) {

			return null;
		}
		return mapper.map(user, UserVo.class);
	}

	@Override
	public void updatePassword(String userId, String createNewpassword) {
		userDao.updatePassword(userId,createNewpassword);
	}

	@Override
	public User queryUserByPhone(String mobile) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultData<Object> register(User user) {
		userDao.saveUsers(user);
		return ResultCode.success("新增用户成功", user);
	}

	@Override
	public void updateUserProperty(String userId, Map<String, Object> property) {
		userDao.updateUserProperty(userId,property);
	}

	@Override
	public List<UserVo> queryUserList(int pageNo, int pageSize, String keyWord) {
		List<UserVo> list = new ArrayList<UserVo>();
		List<User> userList = userDao.queryUserList(pageNo, pageSize, keyWord);
		if (CollectionUtils.isNotEmpty(userList)) {
			for (User user : userList) {
				UserVo vo = mapper.map(user, UserVo.class);
				list.add(vo);
			}
		}
		return list;
	}

	@Override
	public long queryAllUserCount(String keyWord) {
        return userDao.queryAllUserCount(keyWord);
	}

	@Override
	public void deleteUser(String userId) {
		userDao.deleteUserById(userId);
	}

	@Override
	public void updateUserByUserId(String userId, Map<String, Object> map) {
		userDao.updateUserProperty(userId, map);
	}

	@Override
	public void saveUser(UserVo userVo) {
		
	}

	@Override
	public User queryUserByToken(String token) {
		return userDao.queryUserByToken(token);
	}
}
